<template>
  <el-container>
    <el-header>
      <router-link :to="{ name: 'Home' }" class="gotohome"
        ><h1>前端题库网</h1></router-link
      ></el-header
    >
    <el-main>
      <div class="mainbox">
        <el-form
          class="inpbox"
          :model="signupform"
          :rules="formrules"
          ref="formref"
        >
          <h2>注册</h2>
          <el-form-item label="用户名" prop="username">
            <el-input v-model="signupform.username"></el-input>
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input v-model="signupform.password" type="password"></el-input>
          </el-form-item>

          <el-form-item label="再次确认密码" prop="passwordSure">
            <el-input
              v-model="signupform.passwordSure"
              type="password"
            ></el-input>
          </el-form-item>

          <el-form-item class="btn">
            <el-button type="info" plain @click="signup">注册</el-button>
            <!-- 重置表单功能 -->
            <el-button type="info" plain @click="tosignin"
              >有账号，去登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-main>
    <el-footer> xxx科技©2021 All rights reserved admin@xxxxxxxx.com </el-footer>
  </el-container>
</template>

<script>
import { apiCheckName, apiAddUser } from "../api/user";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.signupform.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    var checkName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else if (value.length < 3 || value.length > 20) {
        callback(new Error("请输入长度在 3 到 20个字符!"));
      } else {
        this.checkNameOver(callback);
      }
    };
    return {
      signupform: {
        username: "",
        password: "",
        passwordSure: "",
      },

      formrules: {
        username: [
          { validator: checkName, trigger: "blur" },
          { required: true, message: "请输入密码", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 20, message: "长度在 6 到 20个字符", trigger: "blur" },
        ],
        passwordSure: [
          { validator: validatePass, trigger: "blur" },
          { required: true, message: "请输入密码", trigger: "blur" },
        ],
      },
    };
  },
  created() {},
  methods: {
    signup() {
      this.$refs.formref.validate(async (valid) => {
        if (!valid) return this.$message.error("用户名或密码不符合要求");
        const res = await apiAddUser(this.signupform);
        console.log(res);
        if (res.data.state !== 200) {
          this.$message.error("添加用户失败！");
        }

        this.$message.success("添加成功！");
        this.$router.push("/signin");
      });
    },
    //验证名字是否被注册过
    async checkNameOver(callback) {
      if (this.signupform.username.length < 3) return;

      const res = await apiCheckName(this.signupform.username);
      if (res.data.state === 200) {
        callback();
      } else {
        callback(new Error("用户名被注册过了"));
      }
    },
    tosignin() {
      this.$router.push("/signin");
    },
  },
};
</script>

<style lang="less" scoped>

.el-container {
  width: 100vw;
  height: 100vh;
}
.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}
.el-header h1 {
  margin-left: 20%;
  float: left;
  line-height: 50%;
}
.el-main {
  background-color: #e9eef3;
  color: #333;
  display: flex;
  align-items: center;
}

.mainbox {
  margin: 0 auto;
  width: 30%;
  height: 50%;
  background-color: #b3c0d1;
  display: flex;
  align-items: center;
}
.el-input {
  width: 70%;
}
.inpbox {
  text-align: right;
  margin: 0 auto;
  width: 80%;

  /deep/ .el-form-item__error {
  left: 30%;
}
}
.inpbox h2 {
  text-align: center;
}
.gotohome {
  color: black;
}

a {
  text-decoration: none;
}

</style>